package com.prs.crm.action.hr;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.hr.Leave;
import com.prs.crm.domain.hr.LeaveAudit;
import com.prs.crm.domain.hr.LeaveType;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.exception.InValidDeleteException;
import com.prs.crm.service.hr.LeaveAuditService;
import com.prs.crm.service.hr.LeaveService;
import com.prs.crm.service.hr.LeaveTypeService;

public class LeaveAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Leave leave;
	private LeaveService leaveService;

	private LeaveAudit leaveAudit;
	private LeaveAuditService leaveAuditService;

	private LeaveTypeService leaveTypeService;

	private List<LeaveType> types;

	private PaginationSupport pagination;
	private Integer[] ids;

	private File upload;
	private String uploadFileName;

	private Person nextAuditPerson;
	private String typeIds;

	@Action("listLeave")
	public String execute() {
		return SUCCESS;
	}

	@Action("listLeaveByLeave")
	public String listLeaveByLeave() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Leave.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		if (typeIds != null) {
			criteria.createAlias("type", "ti");
			criteria.add(Restrictions.in("ti.id", ToolKits
					.convertStringAryToIntAry(typeIds.split(","))));
		}
		this.setPagination(this.getLeaveService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listAuditLeave")
	public String listAuditLeave() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				LeaveAudit.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("auditPerson", person));
		criteria.add(Restrictions.eq("state", Constrains.TASK_AUDITING));
		this.setPagination(this.getLeaveService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editLeaveAudit")
	public String editLeaveAudit() {
		return SUCCESS;
	}

	@Action("editLeave")
	public String edit() {
		types = this.getLeaveTypeService().getAllLeaveType();
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveLeave", results = {
			@Result(name = "success", type = "redirect", location = "editLeave"),
			@Result(name = "error", type = "chain", location = "editLeave") })
	public String save() {
		if (leave.getType() == null) {
			addActionError("请假类型不能为空");
			return ERROR;
		}
		if (leaveAudit.getAuditPerson() == null) {
			addActionError("审核人不能为空");
			return ERROR;
		}
		if (leave.getBeginTime() == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		if (leave.getEndTime() == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if(leave.getEndTime().before(leave.getBeginTime()))
		{
			addActionError("结束时间不能在开始时间之前");
			return ERROR;
		}
		this.setPrompt("提交请假成功");
		leave.setState(Constrains.TASK_AUDITING);
		this.getLeaveService().saveLeaves(leave, upload, uploadFileName);

		leaveAudit.setLeave(leave);
		leaveAudit.setState(Constrains.TASK_AUDITING);
		this.getLeaveAuditService().save(leaveAudit);
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "passAudit", results = { @Result(name = "success", type = "redirect", location = "listAuditLeave") })
	public String passAudit() {
		if (nextAuditPerson == null) {
			this.setPrompt("没有下级审核，审核完成!");
			leaveAudit.getLeave().setState(Constrains.TASK_PASS);
			this.getLeaveService().save(leaveAudit.getLeave());
		} else {
			this.setPrompt("审核完成,已提交" + nextAuditPerson.getResume().getName()
					+ "继续审签!");

			LeaveAudit next = new LeaveAudit();
			next.setAuditPerson(nextAuditPerson);
			next.setLeave(leaveAudit.getLeave());
			Person person = AppContext.getAccountContext().currentUser();
			next.setRecorder(person);
			next.setRecordTime(new Date());
			next.setState(Constrains.TASK_AUDITING);
			this.getLeaveAuditService().save(next);
		}

		leaveAudit.setState(Constrains.TASK_PASS);
		this.getLeaveAuditService().saveleaveAudit(leaveAudit, upload,
				uploadFileName);
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "rebutAudit", results = { @Result(name = "success", type = "redirect", location = "listAuditLeave") })
	public String rebutAudit() {
		this.setPrompt("打回请假申请!");
		leaveAudit.setState(Constrains.TASK_NOTPASS);
		leaveAudit.getLeave().setState(Constrains.TASK_NOTPASS);

		this.getLeaveService().save(leaveAudit.getLeave());
		this.getLeaveAuditService().save(leaveAudit);
		return SUCCESS;
	}

	@Action(value = "removeLeave", results = {
			@Result(name = "success", type = "redirect", location = "listLeaveByLeave?typeIds=${typeIds}"),
			@Result(name = "error", type = "chain", location = "listLeaveByLeave") })
	public String remove() {
		try {

			if (ids == null) {
				this.addActionError("请选择要删除的假条");
				return ERROR;
			}
			this.getLeaveService().batchRemove(ids);
			return SUCCESS;
		} catch (InValidDeleteException e) {
			// TODO: handle exception
			this.addActionError("有关联数据,无法删除");
			return ERROR;
		}
	}

	@Action(value = "removeMySubmitLeave", results = {
			@Result(name = "success", type = "redirect", location = "listMySubmitLeave"),
			@Result(name = "error", type = "chain", location = "listMySubmitLeave") })
	public String removeMySubmitLeave() {
		try {

			if (ids == null) {
				this.addActionError("请选择要删除的假条");
				return ERROR;
			}
			this.getLeaveService().batchRemove(ids);
			return SUCCESS;
		} catch (InValidDeleteException e) {
			// TODO: handle exception
			this.addActionError("有关联数据,无法删除");
			return ERROR;
		}
	}

	@Action("listMySubmitLeave")
	public String listSubmitLeave() {
		Person person = AppContext.getAccountContext().currentUser();
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Leave.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Restrictions.eq("recorder", person));
		this.setPagination(this.getLeaveService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "removeMyPassLeave", results = {
			@Result(name = "success", type = "redirect", location = "listMyPassLeave"),
			@Result(name = "error", type = "chain", location = "listMyPassLeave") })
	public String removeMyPassLeave() {
		try {

			if (ids == null) {
				this.addActionError("请选择要删除的假条");
				return ERROR;
			}
			this.getLeaveService().batchRemove(ids);
			return SUCCESS;
		} catch (InValidDeleteException e) {
			// TODO: handle exception
			this.addActionError("有关联数据,无法删除");
			return ERROR;
		}
	}

	@Action("listMyPassLeave")
	public String listPassLeave() {
		Person person = AppContext.getAccountContext().currentUser();
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Leave.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Restrictions.eq("recorder", person));
		criteria.add(Restrictions.eq("state", Constrains.TASK_PASS));
		this.setPagination(this.getLeaveService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "removeMyRebutLeave", results = {
			@Result(name = "success", type = "redirect", location = "listMyRebutLeave"),
			@Result(name = "error", type = "chain", location = "listMyRebutLeave") })
	public String removeMyRebutLeave() {
		try {

			if (ids == null) {
				this.addActionError("请选择要删除的假条");
				return ERROR;
			}
			this.getLeaveService().batchRemove(ids);
			return SUCCESS;
		} catch (InValidDeleteException e) {
			// TODO: handle exception
			this.addActionError("有关联数据,无法删除");
			return ERROR;
		}
	}

	@Action("listMyRebutLeave")
	public String listRebutLeave() {
		Person person = AppContext.getAccountContext().currentUser();
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Leave.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Restrictions.eq("recorder", person));
		criteria.add(Restrictions.eq("state", Constrains.TASK_NOTPASS));
		this.setPagination(this.getLeaveService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewLeave")
	public String view() {
		return SUCCESS;
	}

	@Action("viewLeaveAudit")
	public String viewLeaveAudit() {
		return SUCCESS;
	}

	public Leave getLeave() {
		return leave;
	}

	public void setLeave(Leave leave) {
		this.leave = leave;
	}

	public LeaveService getLeaveService() {
		return leaveService;
	}

	public void setLeaveService(LeaveService leaveService) {
		this.leaveService = leaveService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public LeaveTypeService getLeaveTypeService() {
		return leaveTypeService;
	}

	public void setLeaveTypeService(LeaveTypeService leaveTypeService) {
		this.leaveTypeService = leaveTypeService;
	}

	public void setTypes(List<LeaveType> types) {
		this.types = types;
	}

	public List<LeaveType> getTypes() {
		return types;
	}

	public LeaveAudit getLeaveAudit() {
		return leaveAudit;
	}

	public void setLeaveAudit(LeaveAudit leaveAudit) {
		this.leaveAudit = leaveAudit;
	}

	public LeaveAuditService getLeaveAuditService() {
		return leaveAuditService;
	}

	public void setLeaveAuditService(LeaveAuditService leaveAuditService) {
		this.leaveAuditService = leaveAuditService;
	}

	public void setNextAuditPerson(Person nextAuditPerson) {
		this.nextAuditPerson = nextAuditPerson;
	}

	public Person getNextAuditPerson() {
		return nextAuditPerson;
	}

	public String getTypeIds() {
		return typeIds;
	}

	public void setTypeIds(String typeIds) {
		this.typeIds = typeIds;
	}

}
